<?php

include_once 'common_model.php';		//加载数据库操作类
class Room_Model extends Game_Common_Model
{
	public function __construct()
	{
		header('Content-Type: text/html; charset=utf-8'); 
		parent::__construct();
	}
	
	/************************************************
					common function
	*************************************************/
		
	/*
		获取用户房票
	*/
	public function getRoomTicket($arrData)
	{
		$timestamp = time();
		$result = array();
		
		if(!isset($arrData['account_id']) || $arrData['account_id'] == G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(getRoomTicket):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("account_id");
		}
	
		
		$account_id = $arrData['account_id'];

		$dealerDB = Game_Const::DBConst_Name;
		
		$ticket_where = 'account_id='.$account_id.'';
		$ticket_sql = 'select ticket_count from '.Room_Ticket.' where '.$ticket_where.'';
		$ticket_query = $this->getDataBySql($dealerDB,1,$ticket_sql);
		if(DB_CONST::DATA_NONEXISTENT == $ticket_query)
		{
			$ticket_count = 0;
			
			//默认添加房卡
			$ticket_array['create_time'] = $timestamp;
			$ticket_array['create_appid'] = $open_id;
			$ticket_array['update_time'] = $timestamp;
			$ticket_array['update_appid'] = $open_id;
			$ticket_array['is_delete'] = G_CONST::IS_FALSE;
			$ticket_array['account_id'] = $account_id;
			$ticket_array['ticket_count'] = $ticket_count;
			
			$ticket_id = $this->getInsertID($dealerDB,Room_Ticket, $ticket_array);
		}
		else
		{
			$ticket_count = $ticket_query['ticket_count'];
		}
		
		$result['ticket_count'] = $ticket_count;
		
		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取用户房票");
	}
	
	
	/*
		获取商品
	*/
	public function getGoodsList()
	{
		$timestamp = time();
		$result = array();
		
		$goods_where = 'is_delete=0';
		$goods_sql = 'select goods_id,title,price,ticket_count from '.Payment_Goods.' where '.$goods_where.'';
		$goods_query = $this->getDataBySql(0,$goods_sql);
		if(DB_CONST::DATA_NONEXISTENT != $goods_query)
		{
			foreach($goods_query as $goods_item)
			{
				$array['goods_id'] = $goods_item['goods_id'];
				$array['title'] = $goods_item['title'];
				$array['price'] = $goods_item['price'];
				$array['ticket_count'] = $goods_item['ticket_count'];
				
				$result[] = $array;
			}
		}
		
		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取商品");
	}

    /*
        获取工会房间
    */
    public function getRoomList($arrData)
    {

        if(!isset($arrData['manage_id']) || $arrData['manage_id'] === G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getRoomScoreboard):lack of manage_id"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("manage_id");
        }
        $room_list = [];
        $dealerDB = "dealer_2";

        $flower_list    = $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,game_type from flower_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);
        $landlord_list  = $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,"2" as game_type from landlord_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);
        $sg_list        = $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,game_type from sg_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);
        $gd_mahjong_list= $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,"6" as game_type from gd_mahjong_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);
        $paijiu_list    = $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,"19" as game_type from paijiu_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);
        $eba_list       = $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,"8" as game_type from eba_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);
        $bull_list      = $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,game_type from bull_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);
        $laibull_list   = $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,"53" as game_type from laibull_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);
        $dcx_list       = $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,"20" as game_type from dcx_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);
        $yxx_list       = $this->getDataBySql($dealerDB, 0, 'select room_id,create_time,data_key,room_number,account_id,is_close,"21" as game_type from yxx_room where is_close=0 and create_time>'.(time() - 24 * 60 * 60) .' and account_id = '.$arrData['manage_id']);

        if ($flower_list    != DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$flower_list);
        if ($landlord_list  != DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$landlord_list);
        if ($sg_list        != DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$sg_list);
        if ($gd_mahjong_list!= DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$gd_mahjong_list);
        if ($paijiu_list    != DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$paijiu_list);
        if ($eba_list       != DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$eba_list);
        if ($bull_list      != DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$bull_list);
        if ($laibull_list   != DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$laibull_list);
        if ($dcx_list       != DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$dcx_list);
        if ($yxx_list       != DB_CONST::DATA_NONEXISTENT) $room_list = array_merge($room_list,$yxx_list);

        foreach ($room_list as &$item) {
            switch($item["game_type"])
            {
                case 26: $item['game_name'] = "六人炸金花"; break;
                case 29: $item['game_name'] = "九人炸金花"; break;
                case 24: $item['game_name'] = "明牌炸金花"; break;
                case 25: $item['game_name'] = "血拼炸金花"; break;
                case 30: $item['game_name'] = "十二人金花"; break;
                case 2:  $item['game_name'] = "斗地主"; break;
                case 3:  $item['game_name'] = "三公"; break;
                case 22: $item['game_name'] = "九人三公"; break;
                case 6:  $item['game_name'] = "麻将"; break;
                case 19: $item['game_name'] = "九人牌九"; break;
                case 8:  $item['game_name'] = "二八杠"; break;
                case 59: $item['game_name'] = "九人牛牛"; break;
                case 55: $item['game_name'] = "六人牛牛"; break;
                case 52: $item['game_name'] = "十二人牛牛"; break;
                case 56: $item['game_name'] = "十人牛牛"; break;
                case 58: $item['game_name'] = "九人八倍牛牛"; break;
                case 53: $item['game_name'] = "癞子牛牛"; break;
                case 20: $item['game_name'] = "大吃小"; break;
                case 21: $item['game_name'] = "鱼虾蟹"; break;
                default : $item['game_name'] = ""; break;
            }
            $item['create_time'] = date("m-d H:i",$item['create_time']);

        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$room_list,'result_message'=>"获取未关闭房间");

    }
    public function getManagerRoomScoreList($arrData){
        $timestamp = time();
        $result = array();
        if (!isset($arrData['manage_id'])){
            log_message('error', "function(getRoomScore):lack of manage_id"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("manage_id");
        }

        if (!isset($arrData['game_type']) || empty($arrData['game_type'])){
            $arrData['game_type'] = 26;
        }

        $arrData['start_time'] = strtotime($arrData['start_time']);
        $arrData['end_time'] = strtotime($arrData['end_time']);

        if ($arrData['end_time'] - $arrData['start_time']  > 2*24*60*60){
            log_message('error', "function(getRoomScore):lack of start_time"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$arrData,'result_message'=>"搜索时间跨度不能超过48小时");
        }

        switch($arrData["game_type"])
        {
            case 26: $game_name = "六人炸金花";$room_table = "flower_room"; break;
            case 29: $game_name = "九人炸金花";$room_table = "flower_room"; break;
            case 24: $game_name = "明牌炸金花";$room_table = "flower_room"; break;
            case 25: $game_name = "血拼炸金花";$room_table = "flower_room"; break;
            case 30: $game_name = "十二人金花";$room_table = "flower_room"; break;
            case 2:  $game_name = "斗地主";$room_table = "landlord_room"; break;
            case 3:  $game_name = "三公";$room_table = "sg_room"; break;
            case 22: $game_name = "九人三公";$room_table = "sg_room"; break;
            case 6:  $game_name = "麻将";$room_table = "gd_mahjong_room"; break;
            case 19: $game_name = "九人牌九";$room_table = "paijiu_room"; break;
            case 8:  $game_name = "二八杠";$room_table = "eba_room"; break;
            case 59: $game_name = "九人牛牛";$room_table = "bull_room"; break;
            case 55: $game_name = "六人牛牛";$room_table = "bull_room"; break;
            case 52: $game_name = "十二人牛牛";$room_table = "bull_room"; break;
            case 56: $game_name = "十人牛牛";$room_table = "bull_room"; break;
            case 58: $game_name = "九人八倍牛牛";$room_table = "bull_room"; break;
            case 53: $game_name = "癞子牛牛";$room_table = "laibull_room"; break;
            case 20: $game_name = "大吃小";$room_table = "dcx_room"; break;
            case 21: $game_name = "鱼虾蟹";$room_table = "yxx_room"; break;
            default : $game_name = ""; break;
        }


        if ($room_table) {
            $sql = "SELECT score.*,room.account_id manager_id,room.is_close, room.room_number,'".$game_name."' as game_name FROM room_game_score score LEFT JOIN " . $room_table . " room ON room.room_id = score.room_id ";
            $where = "WHERE score.game_time > " . $arrData['start_time'] . " AND score.game_time <= " . $arrData['end_time'] . " AND score.game_type = " . $arrData['game_type'] . " AND room.account_id = " . $arrData['manage_id'];

            $result = $this->getDataBySql( "dealer_2", 0 , $sql.$where);

        }else{
            log_message('error', "function(getRoomScore):lack of game_type"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("game_type");
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取关闭房间");

    }
    public function getRoomScoreList($arrData){
        $timestamp = time();
        $result = array();
        if (!isset($arrData['manage_id'])){
            log_message('error', "function(getRoomScore):lack of manage_id"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("manage_id");
        }

        if (!isset($arrData['account_id'])){
            log_message('error', "function(getRoomScore):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("account_id");
        }

        if (!isset($arrData['game_type'])){
            $arrData['game_type'] = 26;
        }
        if (!isset($arrData['start_time'])){
            $arrData['start_time'] = $timestamp - 24*60*60;
        }else{
            $arrData['start_time'] = strtotime($arrData['start_time']);
        }
        if (!isset($arrData['end_time'])){
            $arrData['end_time'] = $timestamp;
        }else{
            $arrData['end_time'] = strtotime($arrData['end_time']);
        }

        if ($arrData['end_time'] - $arrData['start_time']  > 2*24*60*60){
            log_message('error', "function(getRoomScore):lack of manage_id"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$arrData,'result_message'=>"搜索时间跨度不能超过48小时");
        }

        switch($arrData["game_type"])
        {
            case 26: $game_name = "六人炸金花";$room_table = "flower_room"; break;
            case 29: $game_name = "九人炸金花";$room_table = "flower_room"; break;
            case 24: $game_name = "明牌炸金花";$room_table = "flower_room"; break;
            case 25: $game_name = "血拼炸金花";$room_table = "flower_room"; break;
            case 30: $game_name = "十二人金花";$room_table = "flower_room"; break;
            case 2:  $game_name = "斗地主";$room_table = "landlord_room"; break;
            case 3:  $game_name = "三公";$room_table = "sg_room"; break;
            case 22: $game_name = "九人三公";$room_table = "sg_room"; break;
            case 6:  $game_name = "麻将";$room_table = "gd_mahjong_room"; break;
            case 19: $game_name = "九人牌九";$room_table = "paijiu_room"; break;
            case 8:  $game_name = "二八杠";$room_table = "eba_room"; break;
            case 59: $game_name = "九人牛牛";$room_table = "bull_room"; break;
            case 55: $game_name = "六人牛牛";$room_table = "bull_room"; break;
            case 52: $game_name = "十二人牛牛";$room_table = "bull_room"; break;
            case 56: $game_name = "十人牛牛";$room_table = "bull_room"; break;
            case 58: $game_name = "九人八倍牛牛";$room_table = "bull_room"; break;
            case 53: $game_name = "癞子牛牛";$room_table = "laibull_room"; break;
            case 20: $game_name = "大吃小";$room_table = "dcx_room"; break;
            case 21: $game_name = "鱼虾蟹";$room_table = "yxx_room"; break;
            default : $game_name = ""; break;
        }


        if ($room_table) {
            $sql = "SELECT score.*,room.account_id manager_id,room.is_close, room.room_number,'".$game_name."' as game_name FROM room_game_score score LEFT JOIN " . $room_table . " room ON room.room_id = score.room_id ";
            $where = "WHERE score.game_time > " . $arrData['start_time'] . " AND score.game_time <= " . $arrData['end_time'] . " AND score.game_type = " . $arrData['game_type'] . " AND room.account_id = 21423";// . $arrData['manage_id'];
            if (isset($arrData["account_id"])) {
                $where .= "AND score.account_id = " . $arrData['account_id'];
            }

            $result = $this->getDataBySql( "dealer_2", 0 , $sql.$where);

        }else{
            log_message('error', "function(getRoomScore):lack of game_type"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("game_type");
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取关闭房间");

    }

	/*
		获取关闭房间积分榜房间
	*/
	public function getRoomScoreboard($arrData)
	{
		$timestamp = time();
		$result = array();
		
		if(!isset($arrData['room_number']) || $arrData['room_number'] == G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(getRoomScoreboard):lack of room_number"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("room_number");
		}
		
		if(!isset($arrData['game_type']) || $arrData['game_type'] === G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(getRoomScoreboard):lack of game_type"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("game_type");
		}
		
		$room_number = $arrData['room_number'];
		$game_type = $arrData['game_type'];

		$dealerDB = "dealer_2";

		$room_status = -1;
		$balance_scoreboard = "";

		//游戏类型 ：1炸金花  2斗地主  3梭哈  4德州  5六人斗牛 6广东麻将   9九人斗牛，10轮庄
		switch($game_type)
		{
			case 26:
			case 29:
			case 24:
			case 25:
            case 30:
				$room_table = "flower_room";
				break;
			case 2:
				$room_table = "landlord_room";
				break;
			case 3:
			case 22:
                $room_table = "sg_room";
                break;
			case 6:
				$room_table = "gd_mahjong_room";
				break;
            case 19:
                $room_table = "paijiu_room";
                break;
            case 8:
                $room_table = "eba_room";
                break;
			case 59:
            case 55:
            case 52:
			case 56:
			case 58:
				$room_table = "bull_room";
				break;
            case 53:
                $room_table = "laibull_room";
                break;
        	case 20:
				$room_table = "dcx_room";
				break;
			case 21:
				$room_table = "yxx_room";
				break;
			default :
				$room_table = "";
				break;
		}
		if($room_table != "")
		{
			if($room_table === "dcx_room")
			{
				$room_sql = 'select room_id,is_close,max_count from dcx_room where room_number="'.$room_number.'" limit 1';
			}
			else if ($room_table === "laibull_room")
			{
				$room_sql = 'select room_id,is_close,max_count,max_times from laibull_room where room_number="'.$room_number.'" limit 1';
			}
			else
			{
				//获取房间id
				$room_sql = 'select room_id,is_close from '.$room_table.' where room_number="'.$room_number.'" limit 1';
			}

			$room_query = $this->getDataBySql($dealerDB,1,$room_sql);
			if(DB_CONST::DATA_NONEXISTENT != $room_query)
			{

				if(isset($room_query['max_count']) ){
					$result['max_count'] = $room_query['max_count'];
				}
				if(isset($room_query['max_times']) ){
					$result['max_times'] = $room_query['max_times'];
				}



				if($room_query['is_close'] == 1)
				{
					$room_id = $room_query['room_id'];
					

					//获取积分榜
					$score_sql = 'select create_time,balance_board,game_num,rule_text from room_scoreboard where room_id='.$room_id.' and game_type='.$game_type.' order by create_time desc limit 1';
					$score_query = $this->getDataBySql($dealerDB,1,$score_sql);
					if(DB_CONST::DATA_NONEXISTENT != $score_query)
					{
						$room_status = 4;	//已关闭

						$total_num = "";
						$rule_text = $score_query['rule_text'];
						$rule_text_array = explode('局/',$rule_text); 
						if(is_array($rule_text_array) && count($rule_text_array) > 0)
						{
							$total_num = $rule_text_array[0];
						}
						

						$name_board = json_decode($score_query['balance_board'],TRUE);

						$balance_scoreboard = array('time'=>$score_query['create_time'], 'scoreboard'=>$name_board,'game_num'=>$score_query['game_num'],'total_num'=>$total_num);
					}
				}
			}
		}
		$result['room_status'] = $room_status;
		$result['balance_scoreboard'] = $balance_scoreboard;

		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取关闭房间积分榜");

	}
	

	/*
		获取代理商游戏socket列表
	*/
	public function getGameSocketList($arrData)
	{
		$timestamp = time();
		$result = array();
		
		if(!isset($arrData['dealer_num']) || $arrData['dealer_num'] == G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(getGameSocketList):lack of dealer_num"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("dealer_num");
		}
		
		$dealer_num = $arrData['dealer_num'];
		$dealerDB = "dealer_".$dealer_num;

		$game_type = -1;
		if(isset($arrData['game_type']) && $arrData['game_type'] != G_CONST::EMPTY_STRING)
		{
			$game_type = $arrData['game_type'];
		}

		//获取游戏列表
		$game_where = 'is_delete=0';
		if($game_type > 0)
		{
			$game_where .= '';
		}

		$game_sql = 'select game_type,domain_host,domain_port from '.Game_List.' where '.$game_where.'';
		$game_query = $this->getDataBySql($dealerDB,0,$game_sql);
		if(DB_CONST::DATA_NONEXISTENT != $game_query)
		{
			foreach($game_query as $game_item)
			{
				$result[$game_item['game_type']] = $game_item['domain_host'].":".$game_item['domain_port'];
			}
		}

		if(count($result) == 0)
		{
			$result = new stdClass;
		}

		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取代理商游戏socket列表");
	}

	
	
}
